﻿Public Class frm_settings

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If Not FBD_Default_File_Location.ShowDialog = Windows.Forms.DialogResult.Cancel Then
            txt_default_file_path.Text = FBD_Default_File_Location.SelectedPath
        End If

    End Sub

    Private Sub cmd_save_Click(sender As Object, e As EventArgs) Handles cmd_save.Click

        Dim _new_settings As New Settings
        Dim _tools As New Core.Tools

        Try

            _new_settings.DefaultDescription = txt_default_desc.Text
            _new_settings.DefaultEncryptionPassword = txt_default_password.Text
            _new_settings.DefaultEncryptionEnabled = chk_default_encrypt.Checked
            _new_settings.DefaultMaxThreads = num_default_max_threads.Value
            _new_settings.DefaultSFDLFileSaveLocation = txt_default_file_path.Text
            _new_settings.DefaultUpper = txt_default_upper.Text
            _new_settings.GenerateFileNameWithDescription = chk_generate_file_name.Checked
            _new_settings.UseFolderNameAsPackagename = chk_usefoldername_as_packaagename.Checked
            _new_settings.ClearSFDLFileQueueAfterCreation = chk_clearqueue_after_create.Checked

            _new_settings.ExcludedFileList = New List(Of String)

            For Each _item In list_excluded_files.Items
                _new_settings.ExcludedFileList.Add(_item.ToString)
            Next

            _tools.XMLSerialize(_new_settings, "settings.xml")

            frm_main.SetSettings = _new_settings

            MessageBox.Show("Einstellungen erfolgreich gespeichert!", "Einstellungen gespeichert", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Me.Close()

        Catch ex As Exception
            MessageBox.Show("Fehler beim speichern der Einstellungen!", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub frm_settings_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim _settings As Settings

        _settings = frm_main.GetSettings

        txt_default_desc.Text = _settings.DefaultDescription
        txt_default_file_path.Text = _settings.DefaultSFDLFileSaveLocation
        txt_default_password.Text = _settings.DefaultEncryptionPassword
        txt_default_upper.Text = _settings.DefaultUpper
        chk_default_encrypt.Checked = _settings.DefaultEncryptionEnabled
        chk_generate_file_name.Checked = _settings.GenerateFileNameWithDescription
        chk_clearqueue_after_create.Checked = _settings.ClearSFDLFileQueueAfterCreation
        chk_usefoldername_as_packaagename.Checked = _settings.UseFolderNameAsPackagename

        list_excluded_files.Items.Clear()
        list_excluded_files.Items.AddRange(_settings.ExcludedFileList.ToArray)

        If _settings.DefaultMaxThreads = 0 Then
            num_default_max_threads.Value = 1
        Else
            num_default_max_threads.Value = _settings.DefaultMaxThreads
        End If

    End Sub

    Private Sub cmd_add_exclude_file_Click(sender As Object, e As EventArgs) Handles cmd_add_exclude_file.Click

        Dim _input As String = String.Empty

        Try

            _input = InputBox("Bitte gebe deine Auschlussregel ein:", "Neue Ausschlussregel", "*.nfo")

            If String.IsNullOrWhiteSpace(_input) Then
                Throw New Exception("Eingabe darf nicht leer sein!")
            End If

            list_excluded_files.Items.Add(_input)

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub cmd_remove_exclude_file_Click(sender As Object, e As EventArgs) Handles cmd_remove_exclude_file.Click

        Try
            If Not list_excluded_files.SelectedItems.Count = 0 Then
                list_excluded_files.Items.Remove(list_excluded_files.SelectedItem)
            End If
        Catch ex As Exception

        End Try

    End Sub
End Class